
-- 父类定义
Vehicle = {}
Vehicle.__index = Vehicle

function Vehicle:new(name)
    local obj = {
        name = name or "Unknown Vehicle"
    }
    setmetatable(obj, Vehicle)
    return obj
end

function Vehicle:start()
    print(self.name .. " engine started")
end

function Vehicle:stop()
    print(self.name .. " engine stopped")
end

function Vehicle:drive()
    print(self.name .. " is driving")
end


-- 更优雅的方式，在子类中保存对父类方法的引用
SportsCar = {}
setmetatable(SportsCar, Vehicle)
SportsCar.__index = SportsCar

-- 保存父类方法的引用
SportsCar.super = Vehicle

--[[
    创建一个新的SportsCar对象
    
    @param name 跑车的名称
    @param brand 跑车的品牌，默认为"Unknown"
    @param horsepower 跑车的马力，默认为0
    @return 返回一个新的SportsCar对象
]]
function SportsCar:new(name, brand, horsepower)
    local obj = Vehicle:new(name)
    setmetatable(obj, SportsCar)
    obj.brand = brand or "Unknown"
    obj.horsepower = horsepower or 0
    return obj
end

--[[
    SportsCar:start - 启动跑车
    该函数负责启动跑车，首先执行跑车特有的启动准备，
    然后调用父类的启动方法，最后输出跑车引擎的轰鸣声。
    
    参数:
        self - SportsCar实例对象
        
    返回值:
        无
]]
function SportsCar:start()
    print("Sports car preparation...")
    -- 通过 super 引用调用父类方法
    self.super.start(self)
    print("Vroom! " .. self.horsepower .. "HP engine roaring!")
end

--[[
    SportsCar:drive - 驾驶跑车
    该函数负责驾驶跑车，首先调用父类的驾驶方法，
    然后输出跑车高速行驶的信息。
    
    参数:
        self - SportsCar实例对象
        
    返回值:
        无
]]
function SportsCar:drive()
    -- 调用父类方法
    self.super.drive(self)
    print("Zooming at high speed with " .. self.horsepower .. " horsepower!")
end

-- 使用示例
local sportsCar = SportsCar:new("911", "Porsche", 450)
sportsCar:start()
print("---")
sportsCar:drive()

--[[
输出结果：
Sports car preparation...
Porsche 911 engine started
Vroom! 450HP engine roaring!
---
Porsche 911 is driving
Zooming at high speed with 450 horsepower!
]]